package com.power.sifa.net.formact.service.impl;

import com.alibaba.fastjson.JSON;
import com.plf.core.base.dao.Ha_userDao;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_applysource;
import com.power.sifa.net.formact.QueryFormDataUtil;
import com.power.sifa.net.formact.service.FormNextService;
import com.power.sifa.respond.model.Apl_plaintiff;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class FormNextRespondImpl implements FormNextService {

    @Resource
    private Ha_userDao ha_userDao;


    @Override
    public void before(String applyid, Map<String, Object> formData) {
        // 保存基本信息
        this.save(applyid, formData);
        // 下一步校验原告必填
        List<Apl_plaintiff> plaintiffList = JSON.parseArray(JSON.toJSONString(QueryFormDataUtil.getSlaveData("apl_plaintiff", formData)), Apl_plaintiff.class);
        if (CollectionUtils.isEmpty(plaintiffList)) {
            throw new RuntimeException("请补充原告信息");
        }
    }

    @Override
    public void after(String applyid, Map<String, Object> formData) {

    }

    @Override
    public void submit(String applyid, Apl_main mainObj) {

    }

    @Override
    public void save(String applyid, Map<String, Object> formData) {
        Map<String, Object> masterObj = QueryFormDataUtil.getMasterData(formData);
        // 1. 审批进行录入的申请, 有录入人id, 查询给对应的主表字段赋值
        String reguserid = (String) masterObj.get("reguserid");
        if (null != reguserid) {
            List<Map> userList = this.ha_userDao.findUserDeptsByUserid(reguserid);
            if (CollectionUtils.isEmpty(userList)) {
                log.error("根据录入人id查询信息失败");
                throw new RuntimeException("根据录入人id查询信息失败");
            }
            Map userObj = userList.get(0);
            // 有录入人就是案件登记
            masterObj.put("source", Tp_applysource.CASEREG);
            masterObj.put("regusername", userObj.get("fullname"));
            masterObj.put("regsectionid", userObj.get("sectionid"));
            masterObj.put("regsectionname", userObj.get("sectionname"));
        }

        Map<String, Object> apl_respondObj = QueryFormDataUtil.getExtendData("apl_respond", formData);
        if(null != apl_respondObj) {
            masterObj.put("acceptcode", apl_respondObj.get("firstcode"));
        }

        List<Apl_plaintiff> plaintiffList = JSON.parseArray(JSON.toJSONString(QueryFormDataUtil.getSlaveData("apl_plaintiff", formData)), Apl_plaintiff.class);
        if (!CollectionUtils.isEmpty(plaintiffList)) {
            List<String> nameList = plaintiffList.stream().map(Apl_plaintiff::getName).collect(Collectors.toList());
            String users = StringUtils.join(nameList, "，");
            apl_respondObj.put("plaintiffname", users);
        }
    }
}
